package com.asialjim.wechat.utils;

import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

/**
 * <h1><em>ASIAL JIM JAVA DOC</em></h1><hr/>
 * <h2>CLASS DESCRIPTION <i>[ NAME: FileUtil ]</i></h2><strong>
 * <p> file util
 * </strong><p><p><em>Copyright &copy; Asial Jim Co., LTD</em><hr/>
 *
 * @author Asial Jim &nbsp;&nbsp; <span>Email:<a href="mailto:asialjim@hotmail.com">asialjim@hotmail.com</a> &nbsp;&nbsp; <a href="asialjim@qq.com">asialjim@qq.com</a></span>
 * @version 1.0.0
 * @since 2021/3/1   &nbsp;&nbsp; JDK 8
 */
public class FileUtil {

    /**
     * <h3>METHOD DESCRIPTION <i>[ NAME: createTmpFile ]</i></h3>
     * create temp file
     * @param inputStream {@link InputStream input stream for file}
     * @param name {@link String file name}
     * @param ext {@link String file extents name}
     * @param tmpDirFile {@link File temp file path}
     * @return {@link File file}
     * @author Asial Jim &nbsp;&nbsp;<span>Email:asialjim@hotmail.com &nbsp;&nbsp; asialjim@qq.com</span>
     * @since 2021/3/1 11:07
     */
    public static File createTmpFile(InputStream inputStream, String name, String ext, File tmpDirFile) throws IOException {
        File resultFile = File.createTempFile(name, '.' + ext, tmpDirFile);

        resultFile.deleteOnExit();
        FileUtils.copyToFile(inputStream, resultFile);
        return resultFile;
    }

    /**
     * <h3>METHOD DESCRIPTION <i>[ NAME: createTmpFile ]</i></h3>
     * create temp file
     * @param inputStream {@link InputStream input stream for file}
     * @param name {@link String file name}
     * @param ext {@link String file extents name}
     * @return {@link File file}
     * @author Asial Jim &nbsp;&nbsp;<span>Email:asialjim@hotmail.com &nbsp;&nbsp; asialjim@qq.com</span>
     * @since 2021/3/1 11:09
     */
    public static File createTmpFile(InputStream inputStream, String name, String ext) throws IOException {
        return createTmpFile(inputStream, name, ext, Files.createTempDirectory("wechat-java-tool-temp-file").toFile());
    }

}